//
//  TKDIYHeader.m
//  Router
//
//  Created by teki on 15/11/11.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "TKDIYHeader.h"

@interface TKDIYHeader ()

@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UIImageView *logo;

@end


@implementation TKDIYHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    self.backgroundColor = RGB(226, 244, 222);
    
    // 设置控件的高度
    self.mj_h = 80;
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:1.0];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    
    
    // logo
    UIImage *image = [UIImage imageNamed:@"device_contect"];
    UIImageView *logo = [[UIImageView alloc] initWithImage:image];
    logo.contentMode = UIViewContentModeScaleAspectFit;
    logo.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
    [self addSubview:logo];
    self.logo = logo;
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.logo.center = CGPointMake(self.mj_w * 0.5,  self.mj_h/2 - 10);
    self.label.frame = CGRectMake(0, 50, self.mj_w, 30);

}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            [self stopAnimation];
            self.label.text = @"下拉刷新数据";
            break;
        case MJRefreshStatePulling:
            [self stopAnimation];
            self.label.text = @"松开手刷新数据";
            break;
        case MJRefreshStateRefreshing:
            self.label.text = @"正在刷新数据...";
            [self startAnimation];
            break;
        default:
            break;
    }
}


- (void)startAnimation
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1.0f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    [self.logo.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}


- (void)stopAnimation
{
    [self.logo.layer removeAnimationForKey:@"rotationAnimation"];
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    // 1.0 0.5 0.0
    // 0.5 0.0 0.5
//    CGFloat red = 1.0 - pullingPercent * 0.5;
//    CGFloat green = 0.5 - 0.5 * pullingPercent;
//    CGFloat blue = 0.5 * pullingPercent;
//    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}


@end
